package broker

import (
	"hft/Database/admin"
	"hft/Http/Services/broker"
	utils "hft/Http/Utils"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type GetUser struct{}

func (*GetUser) GetUserToBroker(c *gin.Context) {

	var userId *admin.User

	c.ShouldBindJSON(&userId)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("你还未登录", c)
	}

	err = broker.GetUserService.GetUserToBroker(int(userId.ID), claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*GetUser) GetGlobalUser(c *gin.Context) {

	var paginate utils.Page

	c.ShouldBind(&paginate)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := broker.GetUserService.GetGlobalUser(&paginate, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)

}

func (*GetUser) GetGlobalUserInfo(c *gin.Context) {

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	var id, _ = strconv.Atoi(c.DefaultQuery("id", "0"))
	if id == 0 {
		utils.FailWithMessage("错误的id", c)
		return
	}

	user, err := broker.GetUserService.GetGlobalUserInfo(uint(id), claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(user, c)
}

// 获取私客池
func (*GetUser) GetPrivatePool(c *gin.Context) {

}

var GetUserController = new(GetUser)
